<?php

class DeliveryorderController extends Base_Controller
{
	

    public function indexAction()
    {
        // action body
    }
    
    public function createAction() {
    	
    	$emapper = new Application_Model_Dao_EntrepotMapper();
        $this->view->entrepots = $emapper ->fetchAll();
    	$customer = new Application_Model_Dao_CustomerMapper();
        $this->view->customers = $customer->fetchAll();
        $request = $this->getRequest();
        
        if($request -> isPost())
        {
//        	if ($form->isValid($request->getPost())) {
				$db = Zend_Db_Table::getDefaultAdapter();
				$db->beginTransaction();
				try {
					$this->logger->debug(print_r($_POST, true));
			 		$do = new Application_Model_BusiInfo_Deliveryorder();
			 		$product = $_POST['steel'];
	        		$do->set_customer($_POST['customerId']);
	        		$do->set_settlementMode($_POST['settlementMode']);
	        		$do->set_invoiceStatus($_POST['invoiceStatus']);
	        		$do->set_entrepot($_POST['entrepotId']);
	        		$do->set_expireDays($_POST['expireDays']);
	        		$do->set_total($_POST['total']);
	        		$mapper  = new Application_Model_Dao_DeliveryorderMapper();
	        		$doId = $mapper->save($do);      		
					foreach($product as $key => $value) {
				        $dodIds = $request->getParam('dodId');
				        $dodId = $dodIds[$key];
				    	$dodMapper = new Application_Model_Dao_DeliveryorderDetailMapper();
				    	if ($value == "dummy") {
				    		
				    		if($dodId != "" ) {
				    			$dodMapper->delete($dodId);
				    		} 
				    	} else {
				    		
				    		$specs = $_POST['specs'];
				    		$amount = $_POST['amount'];
				    		$weight = $_POST['weight'];
				    		$price = $_POST['price'];
				    		$subtotal = $_POST['subtotal'];
				    		$note = $_POST['note'];
				    		$data = array(
								"id" => $dodId,
								'deliveryorder_id'   => $doId,
								'steel_id' => $value,
								'specs' => $specs[$key],
								'amount' => $amount[$key],
								'price' => $price[$key],
								'weight' => $weight[$key],
								'subtotal' => $subtotal[$key],
								'note' => $note[$key],
							);
				    		if ($dodId == null || $dodId == "") {
							  unset($data['id']);
	 						  $dodMapper->getDbTable()->insert($data);
				    		} else {
				    		  $dodMapper->getDbTable()->update($data, array('id = ?' => $dodId));
				    		}
				    	}       		
	            	}
	            	$db->commit();
				} catch (Exception $e) {
            		$db->rollBack();
            		$this->logger->info("提货单创建失败！ \n".$e->getTraceAsString());
            	}
			        		
        		
        		
        		return $this->_helper->redirector('show');
//        	}
        }
    }
   public function showAction() {
       	$doMapper = new Application_Model_Dao_DeliveryorderMapper();
        $this->view->deliverorders = $doMapper->getAllDeliveryOrder();
   }
   
  public function showoneAction() {
		$logger =Zend_Registry::get("logger"); 
  		$id = $this->getRequest()->getParam('deliveryorderId');
  		$logger->log("表示提货单：" . $id , Zend_log::INFO);
        $logger->log(__METHOD__.'('.__LINE__.')'."customer id ：" , Zend_log::DEBUG);
  		$doMapper = new Application_Model_Dao_DeliveryorderMapper();
       	$do = $doMapper->find($id);
        $this->view->do = $do;
        $logger->log(__METHOD__.'('.__LINE__.')'.print_r($do, true) , Zend_log::DEBUG);
        $customerMapper = new Application_Model_Dao_CustomerMapper();
        $logger->log("customer id ：" . $do->get_customerInfo()->get_id() , Zend_log::DEBUG);
        $this->view->customer = $customerMapper ->find($do->get_customerInfo()->get_id());
        
        $dodMapper = new Application_Model_Dao_DeliveryorderDetailMapper();
        $this->view->dod = $dodMapper->fetchAll($id);
                
   }
       
    public function deleteAction() {
    	$id = $this->getRequest()->getParam('deliveryorderId');
    	$dodMapper = new Application_Model_Dao_DeliveryorderDetailMapper();
    	$dodMapper->deleteByDeliveryorder($id);
       	$doMapper = new Application_Model_Dao_DeliveryorderMapper();
        $this->view->deliverorders = $doMapper->delete($id);
        Zend_Registry::get("logger")->log("提货单：" . $id . "删除", Zend_log::INFO);
        return $this->_helper->redirector('show');
    }
    
        
}





